package com.iflysse.controller;


import com.alibaba.fastjson.JSONObject;
import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.ResourceIdBoReq;
import com.iflysse.pojo.bo.req.StudentChapterIdBoReq;
import com.iflysse.pojo.bo.req.StudentLearnRecordBoReq;
import com.iflysse.pojo.bo.resp.ResourceDocAndVideoBoResp;
import com.iflysse.pojo.bo.resp.ResourceHtmlBoResp;
import com.iflysse.pojo.bo.resp.ResourceTestBoResp;
import com.iflysse.pojo.bo.resp.StudentLearnResourceListBoResp;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.ResourceIdVoReq;
import com.iflysse.pojo.vo.req.StudentChapterIdVoReq;
import com.iflysse.pojo.vo.req.StudentLearnRecordVoReq;
import com.iflysse.pojo.vo.resp.*;
import com.iflysse.services.ResourceService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.FileUpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studentLearnResource")
@Api(value = "学生端资源学习的接口")
@Log4j
public class StudentLearnResourceController {



    @Autowired
   private ResourceService resourceService;


    /**
     * 添加资源学习记录
     */
    @PostMapping("/addLearnResource")
    @ApiOperation("添加资源学习记录")
    public CommonVoResp addLearnResource(StudentLearnRecordVoReq studentLearnRecordVoReq){
        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("添加资源学习记录参数bean:"+studentLearnRecordVoReq);
            StudentLearnRecordBoReq studentLearnRecordBoReq=CommonUtil.objToObj(studentLearnRecordVoReq,StudentLearnRecordBoReq.class);
            CommonBoResp commonBoResp= resourceService.addStudentLearnRecord(studentLearnRecordBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("添加资源学习记录响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("添加资源学习记录出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }



    /**
     * 根据章编号加载资源学习列表
     */
    @PostMapping("/loadStudentLearnResource")
    @ApiOperation("根据章编号加载资源学习列表")
    public StudentLearnResourceListVoResp loadStudentLearnResource(StudentChapterIdVoReq studentChapterIdVoReq){
        StudentLearnResourceListVoResp studentLearnResourceListVoResp=new StudentLearnResourceListVoResp();
        try {

            log.info("根据章编号查询查询学习资源情况参数bean:"+studentChapterIdVoReq);
            StudentChapterIdBoReq studentChapterIdBoReq=CommonUtil.objToObj(studentChapterIdVoReq, StudentChapterIdBoReq.class);
            log.info("根据章编号查询查询学习资源情况调用业务接口参数bean:"+studentChapterIdBoReq);
            StudentLearnResourceListBoResp studentLearnResourceListBoResp= resourceService.loadStudentLearnResourceByChapterId(studentChapterIdBoReq);

            log.info("根据章编号查询查询学习资源情况业务方法返回bean:"+studentLearnResourceListBoResp);
            List<StudentLearnResourceListResp> result=new ArrayList<>();
            studentLearnResourceListBoResp.getStudentLearnResourceBoRespList().forEach(studentLearnResourceBoResp -> {
                StudentLearnResourceListResp studentLearnResourceListResp=  CommonUtil.objToObj(studentLearnResourceBoResp,StudentLearnResourceListResp.class);
                result.add(studentLearnResourceListResp);
            });
            studentLearnResourceListVoResp.setResult(result);
            studentLearnResourceListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentLearnResourceListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("根据章编号查询查询学习资源情况返回bean:"+studentLearnResourceListVoResp);

            return studentLearnResourceListVoResp;
        }catch (Exception e){
            log.error("根据章编号查询查询学习资源情况出现异常："+e);
            CommonUtil.printError(studentLearnResourceListVoResp);
            return studentLearnResourceListVoResp;
        }
    }



    /**
     * 根据资源编号查询html 网页学习资源
     */
    @PostMapping("/learnHtmlResource")
    @ApiOperation("根据资源编号查询html 网页学习资源")
    public ResourceHtmlVoResp learnHtmlResource(ResourceIdVoReq resourceIdVoReq) {

        ResourceHtmlVoResp resourceHtmlVoResp=new ResourceHtmlVoResp();
        try{

            log.info("学生端根据资源编号查询html网页学习资源参数："+resourceIdVoReq);

            ResourceIdBoReq resourceIdBoReq= CommonUtil.objToObj(resourceIdVoReq,ResourceIdBoReq.class);
            log.info("学生端根据资源编号查询html网页学习资源业务参数："+resourceIdBoReq);
            ResourceHtmlBoResp resourceHtmlBoResp= resourceService.getHtmlResourceByResourceId(resourceIdBoReq);
            log.info("学生端根据资源编号查询html网页学习资源业务接口响应bean："+resourceHtmlBoResp);
            ResourceHtmlResp result=CommonUtil.objToObj(resourceHtmlBoResp, ResourceHtmlResp.class);
            result.setLearningTime(resourceHtmlBoResp.getLearningTime());
            result.setResourceId(resourceHtmlBoResp.getResourceId());
            result.setResourceTitle(resourceHtmlBoResp.getResourceTitle());
            result.setResourceType(resourceHtmlBoResp.getResourceType());
            resourceHtmlVoResp.setResult(result);
            resourceHtmlVoResp.setCode(ResultEnum.SUCCESS.getCode());
            resourceHtmlVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端根据资源编号查询html网页学习资源响应："+resourceHtmlVoResp);
            return resourceHtmlVoResp;
        }catch (Exception e){
            log.error("学生端根据资源编号查询html网页学习资源接口出现异常："+e);
            CommonUtil.printError(resourceHtmlVoResp);
            return resourceHtmlVoResp;
        }
    }

    /**
     * 学生端学习 练习的接口
     */
    @PostMapping("/learnTestResource")
    @ApiOperation("学生端学习练习的接口")
    public ResourceTestVoResp learnTestResource(ResourceIdVoReq resourceIdVoReq){

        ResourceTestVoResp resourceTestVoResp=new ResourceTestVoResp();

        try{
            log.info("学生端根据资源编号查询试题学习资源参数：" + resourceIdVoReq);
            ResourceIdBoReq resourceIdBoReq = CommonUtil.objToObj(resourceIdVoReq, ResourceIdBoReq.class);
            log.info("学生端根据资源编号查询试题学习资源业务参数：" + resourceIdBoReq);
            ResourceTestBoResp resourceTestBoResp= resourceService.getTestResourceByResourceId(resourceIdBoReq);
            log.info("学生端根据资源编号查询试题学习资源业务接口响应bean："+resourceTestBoResp);
            ResourceTestResp result= CommonUtil.objToObj(resourceTestBoResp,ResourceTestResp.class);
            QuestionResp questionResp=  CommonUtil.objToObj(resourceTestBoResp.getQuestionBoResp(),QuestionResp.class);

            JSONObject js=(JSONObject) JSONObject.parse(questionResp.getQuestionContent());
            Object question=js.get("questions");
            if (question!=null&&!"".equals(question)){
                js.remove("questions");
                js.put("questionItemObjects",question);
            }

            js.remove("analyze");
            js.remove("correct");
            String questionContent= JSONObject.toJSONString(js);
            questionResp.setQuestionContent(questionContent);

            result.setQuestionResp(questionResp);
            result.setLearningTime(resourceTestBoResp.getLearningTime());
            result.setResourceId(resourceTestBoResp.getResourceId());
            result.setResourceTitle(resourceTestBoResp.getResourceTitle());
            result.setResourceType(resourceTestBoResp.getResourceType());
            resourceTestVoResp.setResult(result);
            resourceTestVoResp.setCode(ResultEnum.SUCCESS.getCode());
            resourceTestVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端根据资源编号查询试题学习资源响应："+resourceTestVoResp);
            return resourceTestVoResp;
        }catch (Exception e){
            log.error("学生端根据资源编号查询试题学习资源接口出现异常"+e);
            CommonUtil.printError(resourceTestVoResp);
            return resourceTestVoResp;
        }

    }




    /**
     * 学生端学习 文档视频 接口
     */
    @PostMapping("/learnDocAndVideoResource")
    @ApiOperation("学生端学习文档视频接口")
    public ResourceDocAndVideoVoResp learnDocAndVideoResource(ResourceIdVoReq resourceIdVoReq){

        ResourceDocAndVideoVoResp resourceDocAndVideoVoResp=new ResourceDocAndVideoVoResp();


        try {
            log.info("学生端根据资源编号查询doc和视频学习资源参数：" + resourceIdVoReq);

            ResourceIdBoReq resourceIdBoReq = CommonUtil.objToObj(resourceIdVoReq, ResourceIdBoReq.class);
            log.info("学生端根据资源编号查询doc和视频学习资源业务参数：" + resourceIdBoReq);
            ResourceDocAndVideoBoResp resourceDocAndVideoBoResp = resourceService.getDocAndVideoResourceByResourceId(resourceIdBoReq);
            log.info("学生端根据资源编号查询doc和视频学习资源业务接口响应bean："+resourceDocAndVideoBoResp);
            ResourceDocAndVideoResp result=  CommonUtil.objToObj(resourceDocAndVideoBoResp,ResourceDocAndVideoResp.class);
            result.setResPath(FileUpdateUtil.prefixResources+result.getResPath());
            result.setLearningTime(resourceDocAndVideoBoResp.getLearningTime());
            result.setResourceId(resourceDocAndVideoBoResp.getResourceId());
            result.setResourceTitle(resourceDocAndVideoBoResp.getResourceTitle());
            result.setResourceType(resourceDocAndVideoBoResp.getResourceType());
            resourceDocAndVideoVoResp.setResult(result);
            resourceDocAndVideoVoResp.setCode(ResultEnum.SUCCESS.getCode());
            resourceDocAndVideoVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端根据资源编号查询doc和视频学习资源响应："+resourceDocAndVideoVoResp);
            return resourceDocAndVideoVoResp;
        }catch (Exception e){
            log.error("学生端根据资源编号查询doc和视频学习资源接口出现异常"+e);
            CommonUtil.printError(resourceDocAndVideoVoResp);
            return resourceDocAndVideoVoResp;
        }
    }

}
